package com.marketingexpert.controller.publicmgnt;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.util.WebUtils;

public class LogoutController implements Controller {

	private SnsServiceFactory snsServiceFactory; 
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String backUrl = request.getParameter("backurl");
		
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
		request.getSession().invalidate();
		request.getSession().removeAttribute("sinaAccessToken");
		request.getSession().removeAttribute("weiboUserId");
		if(request.getSession().getAttribute("weiboUserId") == null) {
			response.sendRedirect(WebUtils.getBasepath(request) + "/" + backUrl + ".jsp");
			return null;
		}
		
		if(backUrl.equals("t")) {
			response.sendRedirect(WebUtils.getBasepath(request) + "/" + backUrl + ".jsp");
			return null;
		}
		
		SnsService snsService = getSnsServiceFactory().getService(SnsServiceFactory.SINA);
		snsService.logout((String)request.getSession().getAttribute("weiboUserId"));
		
		response.sendRedirect(WebUtils.getBasepath(request) + "/" + backUrl + ".jsp");
		return null;
	}
	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}
	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}
}
